#!/bin/bash
GREENX="\\033[1;32m"
redx="\\033[1;31m"
bluex="\\033[1;34m"
cyanx="\\033[1;36m"
yellowx="\\033[1;33m"
#_____________________________________________________________________________________________________________________
#
# Use this script to get the starting down time of a specific association - sigtran - spc of your choice .............
# The starting date, ending date, year, MSC have to be specified manually - if not the script will use all defaults values
# ____________________________________________________________________________________________________________________
# Default Values:
# starting date = debut of the current month
# ending date = end of the current month
# month = current month
# year = current year
# MSC = ANTMSC1
# SPC = all international point code
# Brought to you by n00b_who_want_to_learn@n00b1sm.co.ma
# v.0.1
# 12/02/2013
# n00b@localhost 
# TODO: Adding E1 status detection ... LO

clear
androany=$(date)
usage () {
         echo -e "$GREENX" "[*] _______________________________________________________________________________________________________"
         echo -e '\E[37;44m' "\033[*] Usage: Enter the SPC - Year (using long format)-Month (1 to 12), Day (1 to 31) and the ANTMSC (1,2 or 3)\033[0m"
         echo -e "$GREENX" "[*] _______________________________________________________________________________________________________"
         exit 1
  }
#You have to replace these IPs to your own
ip_antmsc1=10.77.144.26
ip_antmsc2=10.77.144.32
ip_antmsc3=10.77.145.26
ip_anthlr1=10.77.144.8
ip_anthlr2=10.77.145.8
pass="p@ssm3\\m\\yw0e1ird0!"
connector () {
    (sleep 1
     echo -e "johnny\r"
     sleep 1
     echo "$pass"
     echo -e "\r"
     sleep 1
     echo -e "\r"
     sleep 1

     echo -e "mml -a\r"
     sleep 1
     echo -e "C7SPP:SP=ALL;\r"
     sleep 1
     sleep 1 ) | telnet ${1} 

     }
check_spc(){
   if    connector ${ip_antmsc1} | grep ${1} > /dev/null 2>&1     ||  connector ${ip_antmsc2} | grep ${1} > /dev/null 2>&1  ||  connector ${ip_antmsc3} | grep ${1} > /dev/null 2>&1  ||  connector ${ip_anthlr1} | grep ${1} > /dev/null 2>&1 ||  connector ${ip_anthlr2} | grep ${1} > /dev/null 2>&1  
         then 
           echo -e "$redx" "[*] SPC Found ..."
      else 
          echo -e "$cyanx" "[*] SPC not found found - 3x1T1n9 ............................"
          exit 1
  fi
}
#printf "\x1b[5m*********************** $androany ***********\x1b[25m"
echo -e "$GREENX" "******************* $androany ******************"
echo -e "$bluex" "[>] The following is meant to sort out all alarms specific to a given point code (of your choice)"
echo -e "$bluex" "[>] Just follow the white r@bb1t ... it owns you :-) ............................................"
echo -n -e "$GREENX" "[>] Please enter the distant point code you wanna play with:"
read spc
  if [[ -z "${spc}" ]]
    then      echo -e "$redx" "*** Your input was empty, guessing that you want to check the international Links .."
        spc=0-
    else     
             echo -e "$redx" "[>] Checking the provided point code ... ... ... ... ... ... ... ... ... ... ... ..."
               check_spc $spc  
             
 fi
echo -n -e "${cyanx}"  "Which MSC do you want to grep the log from:(1=ANTMSC1,2=ANTMSC2,3=ANTMSC3,4=ANTHLR1,5=ANTHLR2)...: "
read antmsc
if [[ -z "${antmsc}" ]]
   then
      echo -e ${cyanx} '*********** The Default MSC is ANTMSC1 **************'
      antmsc=1
fi
if [ "${antmsc}" -eq 1 ]
    then rdir="/home/johnny/ALLIP/ANTMSC1"
         node=antmsc
 elif [ "${antmsc}" -eq 2 ]
    then rdir="/home/johnny/ALLIP/ANTMSC2"
         node=antmsc
 elif [ "${antmsc}" -eq 3 ]
    then rdir="/home/johnny/ALLIP/ANTMSC3"
         node=antmsc
 elif [ "${antmsc}" -eq 4 ]
    then rdir="/home/johnny/ALLIP/ANTHLR1"
         node=anthlr
 elif [ "${antmsc}" -eq 5 ]
    then rdir="/home/johnny/ALLIP/ANTHLR2"
         node=anthlr
 else 
         usage
         exit 1
fi     

echo -n "[>] Enter the year:"
read taona
  if [[ -z "${taona}" ]]
   then 
        taona=`date +%Y`
        echo "****** You didn't specify the Year ... Guessing that it is about the current year :${taona}"
        
  fi
if [ "${taona}" -gt 2013 ]
  then
    echo "** You for sure come from the future .."
    echo "** Are u an Alien ? ****"
    usage
    exit 1
fi
echo -n "[>] Enter the month you wanna play with:"
read volana
if [[ -z "${volana}" ]]
  then
    volana=`date +%m`
    echo  "***** Why the fuck didn't you specify the month ... Taking the ongoing month: ${volana}"
  fi
   
if [ "$volana" -gt 12 ]
  then
   echo -e "${redx}" "[*]- Do you know any month after December ? "
   usage
  exit  1
fi
if [ "${volana}" -lt  10 ]
   then 
     postin=`expr index "$volana" 0`
     if [[ "${postin}" -eq 1 ]]
       then 
          v0l=$volana
       else
      v0l=0$volana
     fi
fi

echo -n "[>] Enter the starting day you wanna play with : from 1-31:"
read day_init
if [[  -z ${day_init} ]]
      then
         echo "**** Taking the default starting date: 01 *******"
         day_init=1
fi

echo -n "[>] The ending day:"
read day_end
if [[ -z ${day_end} ]]
       then 
          echo "***** Taking the default ending date: 31 *******"
          day_end=31
fi 

if [  ${day_end} -lt ${day_init}  -o  ${day_end} -gt 31   ]
  then
       echo  -e "${yellowx}" "Are u fucking kidding me ? People do not count down in real life! Or I don't know, u may know a month that has ${day_end} days ..."
      usage
    exit 1
fi

sleep 1
echo -e "${redx}" "[>] We are going to look during the nb $v0l- month of year $taona between $day_init and $day_end "
echo -n -e "${yellowx}" "[>]... ... ... Press [ ENTER ] to continue ... ... ... ... ... ... ... ... ... ... ... ... ..." && read
sleep 1
echo -e "${yellowx}" "[*] Checking in ${rdir} ............."
cd $rdir
echo -e "${cyanx}" "[x] Filename with Date and Time ::::::::::::: SPC Name ::::::::::: [x]"
for i in `seq ${day_init} ${day_end}`
 do
      if [ ${i} -lt 10 ]
           then
                 if  grep  -n ${spc} allip_${node}${antmsc}_${v0l}_0${i}_${taona}* > /dev/null 2>&1
                  then
                 echo -e "${cyanx}"  "************ ${i}/${v0l}/${taona}************"
             #    grep -q ${spc} allip_antmsc${antmsc}_${v0l}_0${i}_${taona}* &&     grep  -n "${spc}" allip_antmsc${antmsc}_${v0l}_0${i}_${taona}*

                      grep --color=always  -n "${spc}" allip_${node}${antmsc}_${v0l}_0${i}_${taona}* | more
                fi
            else
                  if  grep -n "${spc}"  allip_${node}${antmsc}_${v0l}_${i}_${taona}* > /dev/null 2>&1 
                      then
                echo -e "${cyanx}" "************ ${i}/${v0l}/${taona}************ "
#                  grep -q  "${spc}"  allip_${node}${antmsc}_${v0l}_${i}_${taona}*  && grep  -n "${spc}"  allip_antmsc${antmsc}_${v0l}_${i}_${taona}*
                      grep --color=always  -n "${spc}"  allip_${node}${antmsc}_${v0l}_${i}_${taona}* | more
                 fi
       fi



 done
echo -e "${redx}" "[*]***E*X*I*T*I*N*G*****B*Y*E***"

